local asstes = {
    Asset("ANIM", "anim/knight.zip"),
    Asset("ANIM", "anim/knight_build.zip"),
    Asset("ANIM", "anim/knight_nightmare.zip"),
    Asset("SOUND", "sound/chess.fsb"),
}

local brain = require("brains/knightbrain")

local clockwork_common = require("prefabs/clockwork_common")
local RuinsRespawner = require("prefabs/ruinsrespawner")

SetSharedLootTable("clownhorse",
{
    {"gears",  1.0},
    {"gears",  1.0},
    {"gears",  1.0},
    {"gears",  1.0},
    {"gears",  1.0},
    {"gears",  1.0},
    {"gears",  1.0},
    {"gears",  1.0},
    {"gears",  1.0},
})


local function ShouldSleep(inst)
    return clockwork_common.ShouldSleep(inst)
end

local function ShouldWake(inst)
    return clockwork_common.ShouldWake(inst)
end

local TARGET_DIST = 20
local MUST_TAGS = { "_combat" }
local CANT_TAGS = { "witch", "witchfriend" }
local function Retarget(inst)
    return FindEntity(inst, TARGET_DIST, function(guy)
        return inst.components.combat:CanTarget(guy)
    end, MUST_TAGS, CANT_TAGS)
end

local function KeepTarget(inst, target)
    return clockwork_common.KeepTarget(inst, target) and not target:HasOneOfTags(CANT_TAGS)
end

local function OnAttacked(inst, data)
    clockwork_common.OnAttacked(inst, data)

    if data.target ~= nil and data.target:IsValid() and inst:IsValid() then
        SpawnPrefab("electrichitsparks"):AlignToTarget(data.target, inst, true)
    end
end

local function SetHomePosition(inst)
    inst.components.knownlocations:RememberLocation("home", inst:GetPosition())
end

local function fn()
    local inst = CreateEntity()

    inst.entity:AddTransform()
    inst.entity:AddAnimState()
    inst.entity:AddSoundEmitter()
    inst.entity:AddDynamicShadow()
    inst.entity:AddNetwork()

    MakeCharacterPhysics(inst, 50, .5)

    inst.DynamicShadow:SetSize(1.5, .75)
    inst.Transform:SetFourFaced()

    inst.AnimState:SetBank("knight")
    inst.AnimState:SetBuild("knight_build")

    inst:AddTag("chess")
    inst:AddTag("hostile")
    inst:AddTag("knight")
    inst:AddTag("monster")
    inst:AddTag("witchfriend")
    inst:AddTag("walpurgisnight_friend")

    inst.entity:SetPristine()

    if not TheWorld.ismastersim then
        return inst
    end

    inst.kind = ""

    inst:AddComponent("inspectable")

    inst:AddComponent("combat")
    inst.components.combat.hiteffectsymbol = "spring"
    inst.components.combat:SetAttackPeriod(TUNING.KNIGHT_ATTACK_PERIOD)
    inst.components.combat:SetDefaultDamage(TUNING.KNIGHT_DAMAGE)
    inst.components.combat:SetRetargetFunction(3, Retarget)
    inst.components.combat:SetKeepTargetFunction(KeepTarget)

    inst:AddComponent("electricattacks")
    inst.components.electricattacks:AddSource(inst)

    inst:AddComponent("health")
    inst.components.health:SetMaxHealth(TUNING.CLOWNHORSE_HEALTH)

    inst:AddComponent("sleeper")
    inst.components.sleeper:SetWakeTest(ShouldWake)
    inst.components.sleeper:SetSleepTest(ShouldSleep)
    inst.components.sleeper:SetResistance(3)

    inst:AddComponent("locomotor")
    inst.components.locomotor.walkspeed = TUNING.KNIGHT_WALK_SPEED

    inst:AddComponent("lootdropper")
    inst.components.lootdropper:SetChanceLootTable("clownhorse")

    inst:AddComponent("follower")

    inst:AddComponent("knownlocations")

    inst:AddComponent("drownable")

    MakeMediumBurnableCharacter(inst, "spring")
    MakeMediumFreezableCharacter(inst, "spring")
    MakeHauntablePanic(inst)

    inst:SetStateGraph("SGknight")
    inst:SetBrain(brain)

    inst:ListenForEvent("attacked", OnAttacked)

    inst:DoTaskInTime(0, SetHomePosition)

    return inst
end

return Prefab("clownhorse", fn, asstes)
